var rootPath = getRootPath();
var display_style = isPhoneScreen()?"block":"flex";

$(function() {
	var rleId = getUrlParam("rleId");
	if(rleId == '') {
		layer.alert("认证出错,请求参数有误:)");
	}
	$("#rleId").val(rleId);
	var prevPage = getUrlParam("currentPage");
	$("#role").css("cursor", "pointer");
	
	//回显
	$.ajax({
		url: rootPath + '/verify/uid/rleId/' + rleId + '/dr/1',
		type: 'GET',
		success: function(data) {
			if(data.returnCode == '0000') {
				var obj = data.returnObject;
				$("#phone").val(obj.phone);
				if (obj.sex != null && obj.sex != "") {
					$("input:radio[name=sex][value='" + obj.sex + "']").attr("checked", true);
				}
				//省市区 地址和编码都不为空才回显
				if(obj.address != null && obj.address != "" && obj.location != null && obj.location != "") {					
				 $("#Addr").val(obj.address).data("code",obj.location);
				}
				
				$("#schoolString").val(obj.schoolString);
				$("#creCode").val(obj.creCode);
				$("#hasDreamCenter option[value='" + obj.hasDreamCenter + "']").prop("selected", "selected");
			}
		}
	});
	
	//上一步
	$("#prev").click(function() {
		var creType = $("#sort").val();
		parent.$("#content").prop("src", prevPage + "?rleId="+rleId+"&creType="+creType+"&title=卓越梦想校长报名");
	});
	
	//申请认证
	$("#applyAuth").click(function() {
		if(!checkData()){
			return;
		}
		showLoading();
		$("#excellenceAdreamHeadMasterAuthForm").ajaxSubmit({
			url : rootPath + '/verify/saveOrUpdateVerify',
			type: 'POST',
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				hideLoading();
				if(data.returnCode == '0000'){
					layer.msg("操作成功",{icon:1,time:1000},function() {	
						var callbackUrl = getUrlParam("callbackUrl");//getUrlParam 带转义
						if(callbackUrl){
							parent.document.location.href = callbackUrl;
						}else{
							parent.$("#content").prop("src","applyResult.html?rleId="+rleId+"&title=卓越梦想校长报名");
						}
					});
				} else {
					layer.alert(data.returnMessage);
				}
			},
			error: function (jqXHR, textStatus, errorThrown) {
				hideLoading();
				layer.alert("申请失败,请联系管理员");
			}
		});
		return false;
	});
});

function checkData(){
	//校验手机号
	if($.trim($("#phone").val()) == '') {
		$("#phone").focus();
		showErrMsg("phone", "请输入手机号码");
		return;
	} else {
		if($("#phone").val().indexOf("*") != -1){
			$("#phone").focus();
			showErrMsg("phone","请重新输入手机号");
			return;
		} else if(!checkPhone($("#phone").val())) {
			$("#phone").focus();
			showErrMsg("phone","手机号码格式有误");
			return;
		} else {
			hideMsg("phone");
		}
	}
	//校验性别
	var sex = $("input[name='sex']:checked").val();
	if(!sex || $.trim(sex) == '') {
		$("#sex").focus();
		showErrMsg("sex","请选择性别");
		return;
	} else {
		hideMsg("sex");
	}
	//校验地区
	if($.trim($("#Addr").val()) == '' || $.trim($("#Addr").data("code")) == '') {
		$("#Addr").focus();
		showErrMsg("Addr","请选择地区");
		return;
	} else {
		hideMsg("Addr");
	}
	$("#address").val($("#Addr").val());
	$("#location").val($("#Addr").data("code"));
	//校验学校名称
	if($.trim($("#schoolString").val()) == '') {
		$("#schoolString").focus();
		showErrMsg("schoolString","请输入学校名称");
		return;
	} else {
		hideMsg("schoolString");
	}
	if($("#schoolString").val().length > 200) {
		showErrMsg("schoolString","学校名称输入过长");
		return;
	} else {
		hideMsg("schoolString");
	}
	//校验统一社会信用代码（税号）
	if($.trim($("#creCode").val()) == '') {
		$("#creCode").focus();
		showErrMsg("creCode","请输入统一社会信用代码（税号）");
		return;
	} else {
		hideMsg("creCode");
	}
	if($("#creCode").val().length > 50) {
		showErrMsg("creCode","统一社会信用代码（税号）输入过长");
		return;
	} else {
		hideMsg("creCode");
	}
	//校验学校是否建有梦想中心
	var hasDreamCenter=$("#hasDreamCenter option:selected").val();
	if($.trim(hasDreamCenter) == '') {
		$("#hasDreamCenter").focus();
		showErrMsg("hasDreamCenter","请选择学校");
		return false;
	} else {
		hideMsg("hasDreamCenter");
	}
	return true;
}
